package com.bqtv.sport.ui.im

import android.content.Intent
import android.graphics.Color
import android.text.TextUtils
import android.view.View
import com.bqtv.sport.R
import com.bqtv.sport.app.AppActivity
import com.bqtv.sport.commen.dialog.MessageDialog
import com.bqtv.sport.databinding.ActivitySendRedPackageBinding
import com.bqtv.sport.dialog.PayDialog
import com.bqtv.sport.dialog.WithdrawnDialog
import com.bqtv.sport.ui.mine.SetPayPswActivity
import com.hjq.base.BaseActivity
import com.hjq.base.BaseDialog
import com.hjq.base.app.UserInfoUtils
import com.hjq.base.http.HttpData
import com.hjq.http.EasyHttp
import com.hjq.http.listener.HttpCallbackProxy
import com.tencent.qcloud.tuikit.tuichat.TUIChatConstants
import com.tencent.qcloud.tuikit.tuichat.bean.CustomRedPackageMessage


class SendRedPackageMoneyActivity : AppActivity<ActivitySendRedPackageBinding>() {

    companion object {
        private const val INTEXT_KEY_CONTENT = "content"
        private var listener: OnSendListener? = null
        fun start(activity: BaseActivity, isGroup: Boolean, ID: String, listener: OnSendListener?) {
            this.listener = listener
            val intent = Intent(activity, SendRedPackageMoneyActivity::class.java)
            intent.putExtra("isGroup", isGroup)
            intent.putExtra("ID", ID)
            activity.startActivity(intent)
        }
    }

    override fun getViewBinding() = ActivitySendRedPackageBinding.inflate(layoutInflater)

    var isGroup = false
    var ID = ""
    var pkgType = 3

    override fun initView() {
        isGroup = getBoolean("isGroup")
        ID = getString("ID")!!

        if (!isGroup) pkgType = 1
        binding.llGroup.visibility = if (isGroup) View.VISIBLE else View.GONE
    }

    override fun initData() {

        initListener()
    }

    private fun initListener() {
        setOnClickListener( binding.tvSend)
    }

    override fun onClick(view: View) {
        super.onClick(view)
        when (view) {

            binding.tvSend -> {
                if (TextUtils.isEmpty(binding.etMoney.text.toString())) {
                    toast(R.string.please_input_money)
                    return
                }
                if (isGroup && TextUtils.isEmpty(binding.etNum.text.toString())) {
                    toast(R.string.please_input_num)
                    return
                }

                var text = binding.etText.text.toString()
                var money = binding.etMoney.text.toString().toDoubleOrNull()!!

                var num = 1
                if (isGroup) {
                    num = binding.etNum.text.toString().toIntOrNull()!!
                }
                if (money <= 0) {
                    toast(R.string.money_can_not_0)
                    return
                }
                if (isGroup && num <= 0) {
                    toast(R.string.num_can_not_0)
                    return
                }

                if (pkgType == 1) {
                    //普通红包，获取总金额
                    money = money * num
                }

                if (TextUtils.isEmpty(text)) {
                    text = "恭喜发财，大吉大利"
                }
                //没设置密码
                /*var msgDialog = MessageDialog.Builder(getContext())
                    .setTitle(R.string.kind_reminder)
                    .setMessage(R.string.no_pay_psw_tip)
                    .setListener(object : MessageDialog.OnListener {
                        override fun onConfirm(dialog: BaseDialog?) {
                            SetPayPswActivity.start(getContext())
                        }

                    })
                msgDialog.show()*/
                // 支付密码输入对话框
                var dialog = PayDialog.Builder(this)
                dialog.clickResult = object : PayDialog.Builder.ClickResult {
                    override fun onPswResult(string: String) {

                    }
                }
                dialog.show()
            }
        }

    }

    private fun setView() {
        if (pkgType == 3) {
            binding.tvTypeMoney.text = getString(R.string.amount_of_money)
        }
        if (pkgType == 1) {
            binding.tvTypeMoney.text = getString(R.string.single_amount_of_money)
        }
    }

    fun sendRedPackage(text: String, money: Double, num: Int, password: String) {
        var sendMoney = money
        var sendBeneficiaryId = ""
        if (!isGroup) {
            sendBeneficiaryId = ID
        }

//        EasyHttp.get(this)
//            .api(SendTransactionApi().apply {
//                this.type = pkgType.toString()
//                this.money = sendMoney.toString()
//                this.num = num.toString()
//                this.beneficiaryId = sendBeneficiaryId
//                this.message = text
//                this.passWord = password
//            })
//            .request(object : HttpCallbackProxy<HttpData<String>>(this) {
//                override fun onHttpSuccess(data: HttpData<String>) {
//                    sendRedPackageIM(text, money, num, data.data)
//                }
//            })
    }

    private fun sendRedPackageIM(text: String, money: Double, num: Int, pkgID: String?) {
        if (pkgID == null)
            return
        val customRedPackageMessage = CustomRedPackageMessage()
        customRedPackageMessage.businessID = TUIChatConstants.BUSINESS_ID_CUSTOM_RED_PACKAGE
        customRedPackageMessage.text = text
        customRedPackageMessage.isGroup = isGroup
        customRedPackageMessage.pkgID = pkgID
        customRedPackageMessage.money = money
        customRedPackageMessage.num = num
        customRedPackageMessage.sendUName = UserInfoUtils.getUserInfo().name

        listener?.onSucceed(customRedPackageMessage)
        listener = null
        finish()
    }

    /**
     * 注册监听
     */
    interface OnSendListener {
        /**
         * 成功
         */
        fun onSucceed(content: CustomRedPackageMessage?)

        /**
         * 取消
         */
        fun onCancel() {}
    }
}